<?php

use Monolog\Handler\StreamHandler;
use Monolog\Logger;

if (!function_exists('getOddsResult')) {
    /**
     * 判断足球比赛赔率结果
     * @param  int $type 赔率类型,1胜平负,2进球数,3大小球
     * @param  int $homeScore 赔率类型,1亚指(让球),2欧指(胜平负),3大小球(进球数)
     * @param  int $awaySocre 赔率类型,1胜平负,2进球数,3大小球
     * @param  int $draw 盘口
     * @return string
     * Created by xuqy at 2021/4/16 16:53
     */
    function getOddsResult($type, $homeScore, $awaySocre, $draw = 0)
    {
        if ($type == 1) {
            if ($draw > 0) {
                $socre1 = intval($homeScore);
                $socre2 = intval($awaySocre);
            } else {
                $socre1 = intval($awaySocre);
                $socre2 = intval($homeScore);
            }

            $draw = abs(floatval($draw));

            if ($socre1 - $socre2 > $draw) {
                return '赢';
            }

            if ($socre1 - $socre2 == $draw) {
                return '走';
            }

            if ($socre1 - $socre2 < $draw) {
                return '输';
            }
        }

        if ($type == 2) {
            if (intval($homeScore) > intval($awaySocre)) {
                return '胜';
            }
            if (intval($homeScore) == intval($awaySocre)) {
                return '平';
            }
            if (intval($homeScore) < intval($awaySocre)) {
                return '负';
            }
        }

        if ($type == 3) {
            if (intval($homeScore) + intval($awaySocre) > $draw) {
                return '大';
            }
            if (intval($homeScore) + intval($awaySocre) < $draw) {
                return '小';
            }
            if (intval($homeScore) + intval($awaySocre) == $draw) {
                return '走';
            }
        }

    }
}


if (!function_exists('writeLog')) {
    /**
     * Describe: 写日志
     * @param string $filename 文件名, 比如：【方式1】 filename=order, 并且 byDateDir=true,  则会在 /runtime/logs 目录下生成：order/2022-05-05/log.og 日志
     *                                     【方式2】 filename=order, 并且 byDateDir=false, 则会在 /runtime/logs 目录下生成: order-2022-05-05.log 日志
     *                                说明：filename可以使用/分隔，例如：filename= order/refund, 效果跟上面一直
     * @param mixed $message 要记录的日志信息，可以是string或者array
     * @param bool $byDateDir 是否按日期分目录
     * @return bool
     * Created by crh at 2022/05/20 16:15
     */
    function writeLog(string $filename, $message, bool $byDateDir = false)
    {
        if (is_string($message)) {
            $logMessage = $message;
        } else {
            $logMessage = json_encode($message, JSON_UNESCAPED_UNICODE);
        }
        if ($byDateDir) {
            $path = BASE_PATH . '/runtime/logs/' . $filename . '/' . date('Y-m-d') . '/log.log';
        } else {
            $path = BASE_PATH . '/runtime/logs/' . $filename . '-' . date('Y-m-d') . '.log';
        }

        $log = new Logger('log');
        $log->pushHandler(new StreamHandler($path, Logger::INFO));

        return $log->addRecord(Logger::INFO, sprintf('[message] %s', $logMessage));
    }
}